<?php
/**
 * Created by PhpStorm.
 * User: phperfan
 * Date: 16-9-5
 * Time: 下午11:18
 */

namespace app\index\model;


use think\Cache;
use think\Model;

class Admin extends Model
{
    protected $autoWriteTimestamp = false;
    /**
     * 用户登陆
     */
    public function Login($post){
        $admin = self::where(['username'=>$post['username']])->find();
        if(!$admin){
            return false;
        }
        if($admin->password == md5($post['password'])){
            session('admin',[
                'username' => $admin->username,
                'roleid' => $admin->roleid,
                'email' => $admin->email,
                'realname' => $admin->realname
            ]);
            $admin->lastloginip = $_SERVER['REMOTE_ADDR'];
            $admin->lastlogintime = $_SERVER['REQUEST_TIME'];
            $admin->save();

            session('admin_role',AdminRole::find($admin->roleid));
            session('admin_role_priv',AdminRolePriv::find($admin->roleid));
            if(!Cache::get('admin_menu')){
                $menu = new Menu();
                Cache::set('admin_menu',$menu->getMenu());
            }
            return true;
        }else{
            return false;
        }

    }
}